import { seekElement_id } from '../../../../../Func/uniFunc/Func';

const event_check_operateTag = 'event_check_operate';

/**
 * alt ctrl Shift 关键字
 * @typedef {'Alt'| 'AltLeft'| 'AltRight'|'Control'| 'ControlLeft'| 'ControlRight'|'Shift'| 'ShiftLeft'| 'ShiftRight'} curboardKey
 */

const specialKeyEnumevent = {
  Alt: 'altKey',
  Control: 'ctrlKey',
  Shift: 'shiftKey',
};

class event_check_operate {
  get [Symbol.toStringTag]() {
    return event_check_operateTag;
  }

  constructor() {}

  /**
   * @type {MouseEvent}
   */
  event;

  /**
   * 检测 最终结果
   * @type {boolean}
   */
  checked_result = true;

  cur_event(event) {
    this.checked_result = true;
    this.event = event;
    return this;
  }

  /**
   * 检测 event 出发元素 是否 为 绑定 事件 的元素
   * @returns
   */
  is_itself() {
    this.#set_checked_result(this.event.target == this.event.currentTarget);
    return this;
  }

  /**
   * 是否 左键 点击
   * @returns
   */
  left_click() {
    this.#set_checked_result(this.event.button == 0);
    return this;
  }

  /**
   * 是否 右键 点击
   * @returns
   */
  right_click() {
    this.#set_checked_result(this.event.button == 2);
    return this;
  }

  /**
   * 检测 当前 已经按下的 keyBoard
   * @param {curboardKey[]} beforeKeyB
   * @returns
   */
  specialKey_before_check(beforeKeyB = []) {
    let ret = true;
    for (const key in specialKeyEnumevent) {
      if (this.event[specialKeyEnumevent[key]]) {
        if (beforeKeyB.indexOf(key) > -1) {
          continue;
        }
        ret = false;
      }
    }

    this.#set_checked_result(ret);

    return this;
  }

  /**
   * 检测 当前 按下的 keyBoard
   * @param {curboardKey} curKey
   * @returns
   */
  cur_specialkey_check(curKey) {
    this.#set_checked_result(
      [this.event.key, this.event.code].includes(curKey)
    );

    return this;
  }

  /**
   * 设置 最终检测结果
   * @param {Boolean} bool
   */
  #set_checked_result(bool) {
    if (!this.checked_result) {
      return;
    } else {
      this.checked_result = bool;
    }
  }

  /**
   * 监测 当前 event 操作 是否 在 元素 内部
   * @param {HTMLElement} targetEl
   */
  is_belone_itself(targetEl) {
    let parentEl = seekElement_id(this.event.target, targetEl.id);
    this.#set_checked_result(parentEl == targetEl);
    return this;
  }
}

export { event_check_operate };
